package com.apache.auditRule.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.auditRule.entity.AuditRule;
import com.apache.auditRule.manager.AuditRuleManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class AuditRuleManagerImpl implements AuditRuleManager {

	protected IDao auditRuleDao;
	protected final String entityName = "com.apache.auditRule.entity.AuditRule";

	public void setAuditRuleDao(IDao auditRuleDao) {
		this.auditRuleDao = auditRuleDao;
	}

	public String saveInfo(ParamsVo<AuditRule> vo) throws BusinessException {
		AuditRule auditRule = vo.getObj();
		String infoId = auditRule.getRuleId();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="auditRule_"+infoId;
		auditRule.setRuleId(infoId);
		MethodParam param = new MethodParam("AuditRule", cacheKey, "", entityName);
		param.setVaule(auditRule);
		if (auditRuleDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<AuditRule> vo) throws BusinessException {
		AuditRule auditRule = vo.getObj();
		if (Validator.isNotNull(auditRule.getRuleId())) {
			String cacheKey = "";
			//String cacheKey="auditRule_"+auditRule.getRuleId();
			MethodParam param = new MethodParam("AuditRule", cacheKey, "", entityName);
			param.setVaule(auditRule);
			return auditRuleDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<AuditRule> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="auditRule_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		AuditRule info = (AuditRule) auditRuleDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("AuditRule");
			return auditRuleDao.edit(param);
		} else {
			param.setParams("ruleId", infoId);
			param.setDelete(true);
			return auditRuleDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<AuditRule> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="auditRule_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return auditRuleDao.selectById(param);
	}

	public Object execute(ParamsVo<AuditRule> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<AuditRule> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = auditRuleDao.pageSelect(param);
		return page;
	}

	public List<AuditRule> getList(ParamsVo<AuditRule> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return auditRuleDao.select(param);
	}

	public long countInfo(ParamsVo<AuditRule> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return auditRuleDao.count(param);
	}

	@Override
	public AuditRule getByParams(ParamsVo<AuditRule> vo) {
		AuditRule auditRule = new AuditRule();
		List<AuditRule> list = this.getList(vo);
		if (!Validator.isEmpty(list)) {
			auditRule = list.get(0);
		}
		return auditRule;
	}

	private MethodParam setMethodParams(ParamsVo<AuditRule> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(auditRuleDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		AuditRule auditRule = vo.getObj();
		if (Validator.isNotNull(auditRule.getTableName())) {
			sb.append(" and tableName = :tableName");
			param.setParams("tableName", auditRule.getTableName());
		}
		if (Validator.isNotNull(auditRule.getIfStart())) {
			sb.append(" and ifStart = :ifStart");
			param.setParams("ifStart", auditRule.getIfStart());
		}
		if (Validator.isNotNull(auditRule.getCheckUser())) {
			sb.append(" and checkUser = :checkUser");
			param.setParams("checkUser", auditRule.getCheckUser());
		}
		if (Validator.isNotNull(auditRule.getNext())) {
			sb.append(" and next = :next");
			param.setParams("next", auditRule.getNext());
		}
		if (Validator.isNotNull(auditRule.getLast())) {
			sb.append(" and last = :last");
			param.setParams("last", auditRule.getLast());
		}
		if (Validator.isNotNull(auditRule.getNum())) {
			sb.append(" and num = :num");
			param.setParams("num", auditRule.getNum());
		}
		if (Validator.isNotNull(auditRule.getAuditType())) {
			sb.append(" and auditType = :auditType");
			param.setParams("auditType", auditRule.getAuditType());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
